<?php

namespace app\admin\controller;

use app\admin\model\QuestionsAnswers;
use app\admin\model\questionsCategory;
use app\admin\model\QuestionsList;
use app\admin\service\QuestionsService;
use think\facade\Request;

class Questions extends Base
{
    protected $middleware = ['AdminCheck', 'AdminPermission'];

    //分类
    public function category()
    {
        if (Request::isAjax()) {
            return $this->getJson(QuestionsCategory::getList());
        }
        return $this->fetch();
    }

    public function editCategory($id)
    {
        if (Request::isAjax()) {
            return $this->getJson(QuestionsService::goEditCategory());
        }
        return $this->fetch('', ['data' => QuestionsCategory::find($id)]);
    }

    public function goCategoryStatus()
    {
        return $this->getJson(QuestionsService::goCategoryStatus());
    }

    public function goCategoryRemove()
    {
        return $this->getJson(QuestionsService::goCategoryRemove());
    }

    //考题
    public function list()
    {
        if (Request::isAjax()) {
            return $this->getJson(QuestionsList::getList());
        }
        $this->assign('categoryList', QuestionsCategory::getCategoryAll());
        return $this->fetch();
    }

    public function editQuestion($id)
    {
        if (Request::isAjax()) {
            return $this->getJson(QuestionsService::goEditQuestion());
        }
        $this->assign('categoryList', QuestionsCategory::getCategoryAll());
        $this->assign('typeList', QuestionsList::$typeList);
        return $this->fetch('', ['data' => QuestionsList::find($id)]);
    }

    public function goQuestionStatus()
    {
        return $this->getJson(QuestionsService::goQuestionStatus());
    }

    public function goQuestionRemove()
    {
        return $this->getJson(QuestionsService::goQuestionRemove());
    }

    public function answers($questionId)
    {
        if (Request::isAjax()) {
            return $this->getJson(QuestionsAnswers::getList($questionId));
        }
        return $this->fetch();
    }

    public function editAnswer($qid, $id)
    {
        if (Request::isAjax()) {
            return $this->getJson(QuestionsService::goEditAnswer());
        }
        $this->assign('qid', $qid);
        return $this->fetch('', ['data' => QuestionsAnswers::find($id)]);
    }

    public function goAnswerRemove()
    {
        return $this->getJson(QuestionsService::goAnswerRemove());
    }

    public function getQuestionsBySearch()
    {
        return $this->getJson(QuestionsList::getQuestionsBySearch(Request::post('keyword')));
    }
}